const http =require("http")
const url = require("url")
const fs = require('fs');
const qs = require("querystring")

//处理querystring的内置模块


const server = http.createServer((req,res)=>{
    
    let urlObj =url.parse(req.url,true)
    let pathname = urlObj.pathname
  
    if(pathname =="/"){
        let data =fs.readFileSync("./views/index.html");
        res.write(data);
        res.end();
    }else if(pathname == "/postdata"){
        // 接收post数据，post数据通过流的形式接收，
        // 以此来限定post请求，浏览器地址栏get请求拿不到流数据
        let str ="";
        req.on("data",chunk=>{
            str+= chunk;
        })

        req.on('end',()=>{
            // console.log(str);
            let resObj = qs.parse(str);
            console.log(resObj);
        })

        res.write("返还post的数据")
        res.end();
    }

    
})

server.listen(8889)